home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 102_01.zip / PPONG.C < prev    next >
Text File  |  1993-06-03  |  7KB  |  254 lines

  1. /*
  2.    Polish Pong game for H19/H89, RHH (Robert H. Halstead) August 1980: 
  3.  
  4.    Object is to guide the little ball around the screen by setting up
  5.    and removing blockade sections. All control is via the keypad; "4"
  6.    and "6" cause blockades to be formed at the current position of the
  7.    roving ball, while pressing "5" at the exact moment the ball hits a
  8.    blockade should make that blockade disappear. Oh yes, and the POINT
  9.    of all this is to make the ball hit the little square target--once
  10.    this is done, the square will disappear and reappear somewhere else,
  11.    to be hit again. Go for hitting the target the specified number of
  12.    times AS QUICKLY AS POSSIBLE. Your score is how many seconds you
  13.    took; the lower the better.
  14.    Keys "8" and "2" speed up and slow down the ball; as you get better,
  15.    try it at a faster speed!
  16. */  
  17.  
  18. #include "bdscio.h"
  19.  
  20. #define    MAXX    78        /* horizontal size of board */
  21. #define    MAXY    23        /* vertical size */
  22. #define    MAXTARG    20        /* # of targets per game */
  23. #define    ISPEED    400        /* initial ball speed */
  24. #define    SPEEDINC 100        /* increments/decrements in ball speed */
  25.  
  26. #define    TIMX    50        /* locations of status strings */
  27. #define    TIMY    23
  28. #define    TARGX    30
  29. #define    TARGY    23
  30. #define    SPEEDX    10
  31. #define    SPEEDY    23
  32. #define    BESTX    70
  33. #define    BESTY    23
  34.  
  35. #define    CONINF    1        /* console input FDOS function */
  36.  
  37. #define    MSPS    960        /* "millisecs" per second */
  38.  
  39. #define    QUITCH    3        /* ^C quits the program */
  40. #define    DELETE    0177        /* DELETE restarts the game */
  41.  
  42. #define    XOFF    ('S'&037)    /* flow control chars */
  43. #define    XON    ('Q'&037)
  44.  
  45. #define    EGRAPH    "\033F"        /* H19 escape sequences */
  46. #define    XGRAPH    "\033G"
  47. #define    REGKPM    "\033u\033>"
  48.  
  49. #define VBAR    '`'        /* H19 alternate graphic chars */
  50. #define    HBAR    'a'
  51. #define    SLASH    'x'
  52. #define    BSLASH    'y'
  53. #define    BALL    '^'
  54. #define    TARGET    'i'
  55.  
  56. char board[MAXX][MAXY];        /* board with current layout */
  57.  
  58. int ballx,bally,ballxv,ballyv;    /* state of ball */
  59. int Speed,Dist;            /* speed of ball */
  60. int TargLeft;            /* number of targets left */
  61. int MSecs,Secs;            /* bookkeeping for time elapsed */
  62. int NewTime;            /* nonzero if time has changed */
  63. int Best;            /* best score so far */
  64. int InChar;            /* character read from input */
  65.  
  66. putchx(c)
  67.  int c;
  68.   {    if (++MSecs >= MSPS)
  69.       { MSecs = 0; Secs++; 
  70.         NewTime = 1;
  71.       }
  72.     Dist += Speed;
  73.     if (bios(2))
  74.       { InChar = bios(3) & 0177;
  75.         if (InChar == QUITCH)
  76.           {    InChar = -1;
  77.         outs(0,MAXY-1,XGRAPH);
  78.         prints(CURSORON);
  79.         exit(0);
  80.           }
  81.         if (InChar == XOFF)
  82.           {    while (InChar != XON)
  83.           { while (!bios(2));
  84.             InChar = bios(3) & 0177;
  85.           }
  86.         InChar = -1;
  87.           }
  88.       }
  89.     bios(4,c);
  90.     if (c == '\n') putchx('\r');
  91.   }
  92.  
  93. prints(s)            /* put out a string */
  94.  char *s;
  95.   {    int c;
  96.     while (c = *s++) putchx(c);
  97.   }
  98.  
  99. puts(s)                /* same as prints, but "srand1" needs it */
  100.  char *s;
  101.   {
  102.     prints(s);
  103.   }
  104.  
  105. ouch(ch,x,y)            /* put character at position */
  106.  int ch,x,y;
  107.   {
  108.     putchx(ESC); putchx('Y'); putchx(y+32); putchx(x+32); putchx(ch);
  109.   }
  110.  
  111. outs(x,y,s)            /* put string at position (x,y) */
  112.  int x,y;
  113.  char *s;
  114.   {
  115.     putchx(ESC); putchx('Y'); putchx(y+32); putchx(x+32);
  116.     prints(s);
  117.   }
  118.  
  119. puttarg()
  120.   {    char buff[100];
  121.     sprintf(buff,"\033p%2d\033q",TargLeft);
  122.     outs(TARGX,TARGY,buff);
  123.   }
  124.  
  125. puttime()
  126.   {    char buff[100];
  127.     sprintf(buff,"\033p%3d\033q",Secs);
  128.     outs(TIMX,TIMY,buff);
  129.   }
  130.  
  131. putspeed()
  132.   {    char buff[100];
  133.     sprintf(buff,"\033p%3d\033q",Speed/10);
  134.     outs(SPEEDX,SPEEDY,buff);
  135.   }
  136.     
  137. int moveball()
  138.   {    int i,nx,ny;
  139.     Dist = 0;
  140.     i = InChar;
  141.     if (i > 0)
  142.       { InChar = -1;
  143.         switch (i)
  144.           {    case '4':            /* lay down backslash */
  145.             if (board[ballx][bally] == ' ')
  146.               board[ballx][bally] = BSLASH;
  147.              else putchx(7);
  148.             break;
  149.  
  150.         case '6':            /* lay down slash */
  151.             if (board[ballx][bally] == ' ')
  152.               board[ballx][bally] = SLASH;
  153.              else putchx(7);
  154.             break;
  155.  
  156.         case '5':            /* delete current char */
  157.             i = board[ballx][bally];
  158.             if (i == SLASH || i == BSLASH) board[ballx][bally] = ' ';
  159.             break;
  160.  
  161.         case '8':            /* go faster */
  162.             if (Speed < 1000) { Speed += SPEEDINC; putspeed(); }
  163.             break;
  164.  
  165.         case '2':            /* go slower */
  166.             if (Speed > SPEEDINC+50) { Speed -= SPEEDINC; putspeed(); }
  167.             break;
  168.  
  169.         case DELETE:            /* start a new game */
  170.             return(0);
  171.  
  172.         default:
  173.             putchx(7); break;
  174.           }
  175.       }
  176.     switch (board[ballx][bally])
  177.       { case ' ':    break;
  178.         case VBAR:    ballxv = -ballxv; break;
  179.         case HBAR:    ballyv = -ballyv; break;
  180.         case BSLASH: i = ballxv; ballxv = ballyv; ballyv = i; break;
  181.         case SLASH:    i = ballxv; ballxv = -ballyv; ballyv = -i; break;
  182.         case TARGET:
  183.         if (--TargLeft <= 0) return(0);
  184.         puttarg();
  185.         board[ballx][bally] = ' ';
  186.         do {    nx = rand()%(MAXX-2) + 1;
  187.             ny = rand()%(MAXY-2) + 1;
  188.            } while (board[nx][ny] != ' ');
  189.         board[nx][ny] = TARGET;
  190.         ouch(TARGET,nx,ny);
  191.         break;
  192.       }
  193.     nx = ballx + ballxv;
  194.     ny = bally + ballyv;
  195.     ouch(BALL,nx,ny);
  196.     ouch(board[ballx][bally],ballx,bally);
  197.     if (NewTime) { puttime(); NewTime = 0; }
  198.     ballx = nx; bally = ny;
  199.     while (Dist < (ballyv?22000:10000)) putchx(1);
  200.                     /* further delay to slow ball down */
  201.     return(1);
  202.   }
  203.  
  204. main()
  205.   {    puts("Welcome to Polish Pong!\n"); sleep(10);
  206.     Best = 32767;
  207.     Speed = ISPEED;        /* governs how fast ball moves */
  208.     while (playgame());
  209.   }
  210.  
  211. int playgame()
  212.   {    int i,j;
  213.     char buff[100];        /* temp */
  214.     InChar = -1;            /* initially, no char typed in */
  215.     srand1("\033H\033GType any key to start game:  \033K");
  216.     if (bdos(1) == QUITCH)         /* clear the input character */
  217.         exit();            /* and quit on control-C    */
  218.     InChar = -1;        /* clear space out of input buffer */
  219.     ballx = rand()%(MAXX-2) + 1;
  220.     bally = rand()%(MAXY-2) + 1;
  221.     ballxv = ballyv = 0;
  222.     i = (rand()&2) - 1;
  223.     if (rand()&1) ballxv = i; else ballyv = i;
  224.     for (i = 0; i < MAXX; i++) for (j = 0; j < MAXY; j++) board[i][j] = ' ';
  225.     for (i = 0; i < MAXX; i++) board[i][0] = board[i][MAXY-1] = HBAR;
  226.     for (i = 0; i < MAXY; i++) board[0][i] = board[MAXX-1][i] = VBAR;
  227.     board[0][0] = 'f';        /* special corner pieces */
  228.     board[0][MAXY-1] = 'e';
  229.     board[MAXX-1][0] = 'c';
  230.     board[MAXX-1][MAXY-1] = 'd';
  231.     board[rand()%(MAXX-2)+1][rand()%(MAXY-2)+1] = TARGET;
  232.                     /* place initial target */
  233.     TargLeft = MAXTARG;    /* start with full complement of targets */
  234.     prints(REGKPM); prints(CLEARS); prints(EGRAPH); prints(CURSOROFF);
  235.     for (j = 0; j < MAXY; j++)
  236.       { for (i = 0; i < MAXX; i++) putchx(board[i][j]);
  237.         putchx('\n');
  238.       }
  239.     outs(TIMX-11,TIMY,"\033G\033pTime Used: ");
  240.     outs(TARGX-14,TARGY,"Targets Left: ");
  241.     if (Best < 32767)
  242.       { sprintf(buff,"Best Time: %3d",Best);
  243.         outs(BESTX-11,BESTY,buff);
  244.       }
  245.     outs(SPEEDX-7,SPEEDY,"Speed: \033F\033q");
  246.     putspeed();
  247.     puttarg();
  248.     MSecs = Secs = 0; puttime();
  249.     ouch(BALL,ballx,bally);
  250.     while (moveball());
  251.     if (TargLeft == 0 && Secs < Best) Best = Secs;
  252.     return(1);
  253.   }
  254.